﻿@{
    ViewBag.Title = "订单列表";
    Layout = "~/Areas/Shared/_spEdit.cshtml";
   
}
@using Kaiser.Extensions
@model Kaiser.Modules.Hiner.Models.Order.OrderBase
<style>
    .numberbox {
        width: 40px;
    }
</style>
<form method="post" id="Form1">
    <div class="ftitle">订单基础信息</div>

    <table style="width: 100%">
        <tr>
            <td class="itemtxt" style="text-align: right; width: 15%">请选择供应商：
            </td>
            <td id="SupplierName">
              
                @Html.HiddenFor(m => Model.OrderId)
                @Html.HiddenFor(m => Model.SupplierId)
            </td>
            <td class="itemtxt" style="text-align: right">订单日期：
            </td>
            <td>
                @if (Model.OrderDate.HasValue)
                {
 
                 @Model.OrderDate.Value.ToString("yyyy-MM-dd")
                }
                else
                {
                      @Model.OrderDate
                }
                
            </td>
        </tr>
        <tr>
            <td class="itemtxt" style="text-align: right">订单编号：
            </td>
            <td>
               @Model.OrderNo
            </td>
            <td class="itemtxt" style="text-align: right">联系人：
            </td>
            <td>
               @Model.OrderContacts
            </td>
        </tr>
        <tr>
            <td style="text-align: right">PI编号：
            </td>
            <td>
               @Model.OrderPINo
            </td>
            <td style="text-align: right">上传PI：
            </td>
            <td>
                @Model.OrderPIFile
            </td>
        </tr>
    </table>

    <p></p>
    <div class="ftitle">调用（添加）产品基础数据</div>

    <div id="dg">
    </div>
    <p></p>
    <div class="ftitle">TERMS</div>

    <table>
        <tr>
            <td>1</td>
            <td>Delivery Date: 
                @if (Model.OrderDeliveryDate.HasValue)
                {
                    @Model.OrderDeliveryDate.Value.ToString("yyyy-MM-dd")
                }
                else
                {
                    @Model.OrderDeliveryDate
                }

            </td>
        </tr>
        <tr>
            <td>2</td>
            <td>Payment:@Model.OrderPayMentD% deposit,@Model.OrderPayMentB%balance before shippement</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Goods and Packing must be confirmed before Payment balanced.</td>
        </tr>
        <tr>
            <td>4</td>
            <td>Remark:@Model.OrderClause</td>
        </tr>
    </table>

 

</form>
<div style="display: none">
      <input id="selectSupplier" style="width: 200px" required="true" disabled="disabled" />
</div>
<script type="text/javascript">


    $.extend($.fn.datagrid.methods, {
        editCell: function (jq, param) {
            return jq.each(function () {
                var opts = $(this).datagrid('options');
                var fields = $(this).datagrid('getColumnFields', true).concat($(this).datagrid('getColumnFields'));
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor1 = col.editor;
                    if (fields[i] != param.field) {
                        col.editor = null;
                    }
                }
                $(this).datagrid('beginEdit', param.index);
                for (var i = 0; i < fields.length; i++) {
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
                }
            });
        }
    });

    $.extend($.fn.datagrid.defaults.editors, {
        numberspinner: {
            init: function (container, options) {
                var input = $('<input type="text">').appendTo(container);
                return input.numberspinner(options);
            },
            destroy: function (target) {
                $(target).numberspinner('destroy');
            },
            getValue: function (target) {

                var obj = $('#dg');
                var productPrice = obj.datagrid('getRows')[editIndex]['ProductPrice'];
                var val = $(target).numberspinner('getValue');

                obj.datagrid('getRows')[editIndex]['ProductActualPrice'] = val * productPrice;


                return val;
            },
            setValue: function (target, value) {
                $(target).numberspinner('setValue', value);
            },
            resize: function (target, width) {
                $(target).numberspinner('resize', width);
            }
        }
    });

    function updateTotal() {

        
    }

    $(document).ready(function () {
        

        var g = $('#selectSupplier').combobox({
            panelWidth: 500,
            url: '@Url.Action("GetSupplierList", "SupplierManage", new { area = "Hiner" })',
            valueField: 'SupplierId',
            textField: 'SupplierName',
            mode: 'remote',
            multiple: false,
            onLoadSuccess:function() {
                var txt = $('#selectSupplier').combobox('getText');
                $("#SupplierName").append(txt);
            },
            onSelect: function () {

                $("#SupplierId").val($('#selectSupplier').combobox('getValue'));


            },
            panelHeight: 'auto'
        });

        g.combobox("setValue", $("#SupplierId").val());



        
        var dgData = '@(new HtmlString(Model.OrderDetailList.ToJson()))' ;
    
        $('#dg').datagrid({

            singleSelect: true,
            showFooter: true,
            onLoadSuccess:function() {
                 
            },
            columns: [[
                {
                    field: 'ProductId',
                    title: 'Item',
                    width: 200,
   
                    formatter: function (value, row) {
                        return row.ProductName;
                    } 
                     
                },
                {
                    field: 'ProductHDECode',
                    title: 'HDE Code',
                    width: 120
 
                },
                {
                    field: 'ProductHinerCode',
                    title: 'HINER Code',
                    width: 120 
                },
                { field: 'ProductOERef', title: 'OE Ref.', width: 120 },
                { field: 'ProductPrice', title: 'Price', width: 40},
                { field: 'Quantity', title: 'Qty', width: 80, editor: 'numberspinner' },
                { field: 'ProductActualPrice', title: 'Sum', width: 50 }
            ]],
            data: $.parseJSON(dgData)
 
        });

        // update footer rows with new data
        $('#dg').datagrid('reloadFooter', [
            { Quantity: @Model.OrderDetailList.Sum(x => x.Quantity), ProductActualPrice: @Model.OrderDetailList.Sum(x => x.ProductActualPrice), ProductPrice: 'Total' }
        ]);
      //  append();
    });

</script>

 
